#include <iostream>
#include <unistd.h>
#include <ctime>

int gval = 100;

//新线程
void* threadStart(void* args)
{ 
    while(1)
    {
        std::cout << "new thread running..." << " ,pid: " << getpid() << " ,gval: "<< gval << " ,&gval" << &gval << std::endl;
        sleep(1);

        // int x = rand() % 10;
        // if(x == 0)
        // {
        //     int* p =nullptr;
        //     *p = 100;
        // }
    } 

}

int main()
{
    srand(time(nullptr));
    pthread_t tid1;
    pthread_create(&tid1, nullptr, threadStart, (void *)"thread-new");

    pthread_t tid2;
    pthread_create(&tid2, nullptr, threadStart, (void *)"thread-new");

    pthread_t tid3;
    pthread_create(&tid3, nullptr, threadStart, (void *)"thread-new");

    //主线程
    while(1)
    {
        sleep(1);
        std::cout << "main thread running..." << " ,pid: " << getpid() << " ,gval: "<< gval << " ,&gval" << &gval << std::endl;
        gval++;
    }
    return 0;
}